<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" width="500px" >
    <el-form ref="form" :model="form" size="small" label-width="160px">
      <el-form-item label="请选择导出模板">
        <el-select v-model="bindPrintId" placeholder="请选择模板" @change="printTemplateChange">
          <el-option
            v-for="(item) in printMainList"
            :key="item.id"
            :label="item.reportName"
            :value="item.id"/>
        </el-select>
        <el-button
          v-loading="exportLoading"
          class="filter-item"
          size="mini"
          type="warning"
          icon="el-icon-download"
          @click="doExport"
        >导出</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getPrintByReportCode } from '@/api/system/sysPrint'
import { postRequestDownload } from '@/api/data'

export default {
  components: { },
  props: {
    sup_this: {
      type: Object,
      default: null,
      required: true
    },
    exportUrl: {
      type: String,
      default: '',
      required: true
    },
    reportCode: {
      type: String,
      default: '',
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      exportLoading: false,
      printMainList: [],
      bindPrintId: '',
      form: {
        printMain: {
        },
        reportType: 'xlsx' // xlsx/pdf
      }
    }
  },
  created() {
  },
  methods: {
    printTemplateChange(bindPrintId) {
      if (!bindPrintId) return
      this.form.printMain = this.printMainList.filter(value => {
        return value.id === bindPrintId
      })[0]
    },
    getPrintByReportCode(formData = null) {
      if (formData) {
        this.form = formData
      }
      getPrintByReportCode(this.reportCode).then(res => {
        this.printMainList = res.data
        this.form.printMain = this.printMainList[0]
        this.bindPrintId = this.form.printMain.id
      })
    },
    doExport() {
      this.$confirm('确认要导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.exportLoading = true
        this.form['pageBean'] = null
        const exportFileName = Date.parse(new Date()) + '.xlsx'
        postRequestDownload(this.exportUrl, this.form, exportFileName).then(res => {
          this.exportLoading = false
        }).catch(err => {
          this.exportLoading = false
          console.log(err)
        })
      }).catch(() => {
        this.exportLoading = false
      })
    },
    cancel() {
      this.resetForm()
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
